import Vue from "vue";
import VueRouter from "vue-router"
import publicRoute from "@/router/config";
import store from "@/store";
import StringUtils from "@/utils/StringUtils";

Vue.use(VueRouter);

let router = new VueRouter({
    // https://router.vuejs.org/zh/api/#mode
    mode: 'hash',
    routes: publicRoute
});

router.beforeEach((to, from, next) => {
    console.log('to', to);
    console.log('from', from);
    console.log('next', next);
    if (to.name === 'login' || to.name === 'pageNotFound') {
        next();
        return;
    }
    if (StringUtils.isNotBlank(store.state.accessToken)) {
        next();
    } else {
        let query = {};
        if (to.name !== 'home') {
            query.redirect = to.path;
        }
        next({name: 'login', query});
    }
})

export default router;
